package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.BusinessDaoImpl;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) {
        List<Deliveryaddress> list = null;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            list = deliveryAddressDao.listDeliveryAddressByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }

        return list;
    }

    @Override
    public List<Deliveryaddress> getDeliveryAddressById(Integer daId) {
        List<Deliveryaddress> list = null;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            list = deliveryAddressDao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }

        return list;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        int save =0;
        try {
            save = deliveryAddressDao.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }

        return save;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        int isRight =0;
        try {
            isRight = deliveryAddressDao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }

        return isRight;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        int del =0;
        try {
             del = deliveryAddressDao.removeDeliveryAddress(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return del;
    }
}
